package edu.chinasoft.servlet;

import edu.chinasoft.domain.User;
import edu.chinasoft.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletForRegister",value = "/servlet/servletforregister")
public class ServletForRegister extends HttpServlet {
    private UserService userService;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取页面提交的内容
        String userName = request.getParameter("username");
        String userPassword = request.getParameter("userpassword");


        //封装到javabean
        User user = new User();
        user.setUserName(userName);
        user.setUserPasswrod(userPassword);
        user.setUserDesc("web");
        user.setUserFlag("1");

        //初始化servelt里关于服务层方法成员变量
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        UserService userServiceImp = wac.getBean("userService", UserService.class);
        userService=userServiceImp;
        //调用服务层方法
        this.userService.registerUser(user);

        //根据执行结果实现页面跳转
        response.sendRedirect(""+request.getContextPath()+"/success.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
